package snappy

import (
	"io"
	"sync"

	"github.com/golang/snappy"
)

// pool
type Snappy struct {
	reader sync.Pool
	writer sync.Pool
}

func NewSnappy() *Snappy {
	s := &Snappy{
		sync.Pool{},
		sync.Pool{},
	}
	s.reader.New = func() interface{} {
		return snappy.NewReader(nil)
	}
	s.writer.New = func() interface{} {
		return snappy.NewBufferedWriter(nil)
	}
	return s
}

func (s *Snappy) Writer(writer io.Writer) io.WriteCloser {
	//return snappy.NewBufferedWriter(writer)
	w := s.writer.Get().(*snappy.Writer)
	w.Reset(writer)
	return w
}

func (s *Snappy) ReleaseWriter(writer io.Writer) {
	s.writer.Put(writer)
}

func (s *Snappy) Reader(reader io.Reader) io.Reader {
	//return snappy.NewReader(reader)
	r := s.reader.Get().(*snappy.Reader)
	r.Reset(reader)
	return r
}

func (s *Snappy) ReleaseReader(reader io.Reader) {
	s.reader.Put(reader)
}
